package com.example.foodclient;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class CommentsAddAcitivity extends AppCompatActivity {

    private EditText addCommentet;
    private Button addCommentBtn;
    private Button backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments_add_acitivity);

        initView();
        initEvent();
    }

    private void initEvent() {
        addCommentBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addComment();
            }
        });
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void addComment() {
        Intent intent = getIntent();
        final int foodId = intent.getIntExtra("foodId", 0);
        final int userId = intent.getIntExtra("userId", 0);
        final String comment = addCommentet.getText().toString();
        new Thread(new Runnable() {


            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient().newBuilder()
                            .build();
                    MediaType mediaType = MediaType.parse("application/json");
                    JSONObject jsonObject = new JSONObject();

                    jsonObject.put("comment", comment);
                    jsonObject.put("foodid", foodId);


                    //获取当前时间
                    Timestamp timestamp = new Timestamp(new Date(System.currentTimeMillis()).getTime());

                    jsonObject.put("postdate", timestamp);
                    jsonObject.put("userid", userId);
                    RequestBody body = RequestBody.create(mediaType, jsonObject.toString());
                    Request request = new Request.Builder()
                            .url("http://10.2.64.75:8080/foodserver/comment/add")
                            .method("POST", body)
                            .addHeader("Content-Type", "application/json")
                            .build();

                    Response response = client.newCall(request).execute();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(CommentsAddAcitivity.this, "发表成功", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initView() {
        addCommentet = findViewById(R.id.cm_add_comment_et);
        addCommentBtn = findViewById(R.id.add_comment_btn);

        backBtn = findViewById(R.id.back_btn);
    }
}
